package org.qf.dao.impl;

import org.qf.dao.SmbmsGoodsDao;
import org.qf.pojo.Goods;
import org.qf.pojo.SmbmsBillItem;
import org.qf.utils.BaseDao;

import java.sql.Connection;
import java.util.List;

/**
 * @author zzzyyystart
 * @create 2022-05-11-20:06
 */
public class SmbmsGoodsDaoimpl extends BaseDao implements SmbmsGoodsDao {
    @Override
    public List<Goods> showGoods(String classifyName) {
        Connection conn = null;
        try {
            conn = getConnection1();
            StringBuffer sql1 = new StringBuffer("select `id`,`name`,`price`,`sales`,`stock`,`img_path` imgPath from ");
            StringBuffer append = sql1.append(classifyName);
            String sql = append.toString();
            return queryForList(conn, sql, Goods.class);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return null;
    }

    @Override
    public int updateGoods(String classifyName, Goods goods) {
        Connection conn = null;
        try {
            conn = getConnection1();
            StringBuffer str1 = new StringBuffer("update ");
            StringBuffer str2 = str1.append(classifyName);
            str2.append(" set  `name`=?, `price`=?,`sales`=?,`stock`=? ,`img_path`=? where id=?");
            String sql = str2.toString();
            return update(conn, sql, goods.getName(), goods.getPrice(), goods.getSales(),
                    goods.getStock(), goods.getImgPath(), goods.getId());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return -1;
    }

    @Override
    public Goods showGoodForOne(String classifyName, String goodsId) {
        Connection conn = null;
        try {
            conn = getConnection1();
            StringBuffer str1 =
                    new StringBuffer("select `id`,`name`,`price`,`sales`,`stock`,`img_path` imgPath from ");
            StringBuffer str2 = str1.append(classifyName);
            str2.append(" where id=?");
            String sql = str2.toString();
            return queryForOne(conn, sql, Goods.class, goodsId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return null;
    }

    @Override
    public int addGoods(String classify, Goods goods) {
        Connection conn = null;
        try {
            conn = getConnection1();
            StringBuffer str1 = new StringBuffer("insert into ");
            StringBuffer str2 = str1.append(classify);
            StringBuffer str3 = str2.append("(`name`,`price`,`sales`,`stock`,`img_path`) value(?,?,?,?,?)");
            String sql = str3.toString();
            String sql2="insert into t_goods(`name`,`price`,`sales`,`stock`,`img_path`) value(?,?,?,?,?)";
            update(conn,sql2,goods.getName(), goods.getPrice(), 0, goods.getStock(), goods.getImgPath());
            return update(conn, sql, goods.getName(), goods.getPrice(), 0, goods.getStock(), goods.getImgPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return -1;

    }

    @Override
    public int deleteGoods(String goodsId, String classifyName) {
        Connection conn = null;
        try {
            conn = getConnection1();
            StringBuffer str1 = new StringBuffer("delete from ");
            StringBuffer str2 = str1.append(classifyName);
            StringBuffer str3 = str2.append(" where id=?");
            String sql = str3.toString();
            return update(conn, sql, goodsId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(conn);
        }
        return -1;
    }
}
